package util;

// 制作 JDBC 工具类

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class jdbc_utils {
    private static String Driver = null;
    private static String url = null;
    private static String username = null;
    private static String password = null;


    //使用静态代码块，当类被加载时，静态代码块也会被加载，而不需要新建对象
    static {
        Properties properties = new Properties();
        try {

            InputStream inputStream = (jdbc_utils.class.getClassLoader().getResourceAsStream("database.properties"));

            properties.load(inputStream);
            Driver = properties.getProperty("Driver");
            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");

            //1、驱动只用加载一次
            Class.forName(Driver);

        } catch (Exception e) {
            e.printStackTrace();
            e.printStackTrace();
        }

    }

    //2、获取连接
    public static Connection get_connection() throws SQLException {
        return DriverManager.getConnection(url, username, password);


    }


    //3、释放资源

    public static void release(Connection connection, Statement statement, ResultSet resultSet) throws SQLException {

        if (resultSet != null) {
            resultSet.close();
        }

        statement.close();
        connection.close();


    }

}
